﻿var comboBox = null;
$(function () {
    $("#departmentType").ligerComboBox({
        cancelable: false,
        width: 250,
        valueFieldID: 'hdnDepartmentType',
        onSelected: function (value) {
            bindParentDepartment(value);
        }
    });

    bindParentDepartment($("#hdnDepartmentType").val());
});

function bindParentDepartment(departmentType) {

    $('#tdDepartment').html('<input type="text" id="parentId" style="width:250px;" />');

    comboBox = $("#parentId").ligerComboBox({
        width: 250,
        selectBoxWidth: 250,
        selectBoxHeight: 200,
        valueField: 'DepartmentCode',
        textField: 'DepartmentName',
        valueFieldID: 'hdnParentId',
        treeLeafOnly: false,
        tree: {
            url: '/Sysbase/Department/GetParentDepartment?ram' + Math.random(),
            parms: { departmentType: departmentType },
            ajaxType: 'get',
            idFieldName: "DepartmentCode",
            parentIDFieldName: "ParentId",
            textFieldName: "DepartmentName",
            single: true,
            onSuccess: function (data) {
                $(".l-tree").css('width', '250px');
                if ($("#parentId").val() != "0" && $("#parentId").val() != "") {
                    $("#parentId").ligerComboBox().setValue($("#parentId").val());
                }
                else {
                    $("#parentId").val('');
                }
            }
        }
    });
}

function save() {
    var id = $("#hdnDeptId").val();
    var departmentType = $("#hdnDepartmentType").val();
    var departmentCode = $("#txtDepartmentCode").val();
    var departmentName = $("#txtDepartmentName").val();
    var parentId = $("#hdnParentId").val();
    var manager = $("#txtManager").val();
    var telephone = $("#txtTelephone").val();
    var sort = $("#txtSort").val();
    var location = $("#txtLocation").val();
    var remarks = $("#txtRemarks").val();
    var status = $('input[name="status"]:checked').val();

    if (IsEmpty(departmentName)) {
        $.ligerDialog.error("部门名称不能为空");
        $("#txtDepartmentName").focus();
        return;
    }

    if (IsEmpty(departmentCode)) {
        $.ligerDialog.error("部门编码不能为空");
        $("#txtDepartmentCode").focus();
        return;
    }

    var dialog = frameElement.dialog;
    $.ajax({
        url: "/Sysbase/Department/Save",
        type: "post",
        data: {
            Id: id,
            DepartmentType: departmentType,
            DepartmentCode: departmentCode,
            DepartmentName: departmentName,
            ParentId: parentId,
            Manager: manager,
            Telephone: telephone,
            Sort: sort,
            Location: location,
            Remarks: remarks,
            Status: status
        },
        success: function (r) {
            var r = $.parseJSON(r);
            if (r.Result == 1) {
                parent.$("#btnSearch").click();
                closeDialog();
            }
            else if (r.Result == 0) {
                $.ligerDialog.error(r.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

function closeDialog() {
    frameElement.dialog.close();//关闭dialog
}